package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public TextView result;
    public Button ac;
    public Button zhengfu;
    public Button baifenbi;
    public Button chu;
    public Button seven;
    public Button eight;
    public Button nine;
    public Button cheng;
    public Button four;
    public Button five;
    public Button six;
    public Button jian;
    public Button one;
    public Button two;
    public Button three;
    public Button jia;
    public Button zero;
    public Button dian;
    public Button dengyu;
    public EditText username;
    public EditText password;
    public Button login;
    public Button ret;
    public final String TAG="MainActivity";
    public int n1=-1;
    public int n2=-1;
    public String n3="";
    public int jieguo;
    public String n4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = findViewById(R.id.result);
        ac = findViewById(R.id.ac);
        username =  findViewById(R.id.username);
        password = findViewById(R.id.psw);
        login = findViewById(R.id.login);
        zhengfu = findViewById(R.id.zhengfu);
        baifenbi = findViewById(R.id.baifenbi);
        chu = findViewById(R.id.chu);
        seven = findViewById(R.id.seven);
        eight = findViewById(R.id.eight);
        nine = findViewById(R.id.nine);
        cheng = findViewById(R.id.cheng);
        four =  findViewById(R.id.four);
        five =  findViewById(R.id.five);
        six =  findViewById(R.id.six);
        jian =  findViewById(R.id.jian);
        one =  findViewById(R.id.one);
        two =  findViewById(R.id.two);
        three =  findViewById(R.id.three);
        jia =  findViewById(R.id.jia);
        zero =  findViewById(R.id.zero);
        dian =  findViewById(R.id.dian);
        dengyu =  findViewById(R.id.dengyu);
        ret = findViewById(R.id.ret);


        if(result==null){
            Log.e(TAG, "onCreate: this is from MainActivity" );
        }
        zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=0;
                }
                else
                    n2=0;
                result.setText("0");
            }
        });
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=1;
                }
                else
                    n2=1;
                result.setText("1");
            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=2;
                }
                else
                    n2=2;
                result.setText("2");
            }
        });
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=3;
                }
                else
                    n2=3;
                result.setText("3");
            }
        });
        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=4;
                }
                else
                    n2=4;
                result.setText("4");
            }
        });
        five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=5;
                }
                else
                    n2=5;
                result.setText("5");
            }
        });
        six.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=6;
                }
                else
                    n2=6;
                result.setText("6");
            }
        });
        seven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=7;
                }
                else
                    n2=7;
                result.setText("7");
            }
        });
        eight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=8;
                }
                else
                    n2=8;
                result.setText("8");
            }
        });
        nine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(n1==-1)
                {
                    n1=9;
                }
                else
                    n2=9;
                result.setText("9");
            }
        });

        jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n3="+";
            }
        });
        jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n3="-";
            }
        });
        cheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n3="*";
            }
        });
        chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n3="/";
            }
        });

        ac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                n1=-1;
                n2=-1;
                n3="";
                jieguo=0;
                result.setText("0");
            }
        });


        dengyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (n3){
                    case "+":
                        jieguo=n1+n2;
                        break;
                    case "-":
                        jieguo=n1-n2;
                        break;
                    case "/":
                        jieguo=n1/n2;
                        break;
                    case "*":
                        jieguo=n1*n2;
                        break;
                }

                result.setText(jieguo+"");
            }
        });


        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String u = username.getText().toString();
                String p = password.getText().toString();
                if(u.equals("")||p.equals("")){
                    Toast.makeText(MainActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                }else{
                    if(u.equals("123") && p.equals("123")){
                        Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(MainActivity.this, "登录失败，请检查用户名和密码是否正确", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        ret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });




    }





}